package cn.cqie.attendance.common.controller;


import cn.cqie.attendance.common.service.AttachmentService;
import cn.cqie.attendance.common.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author FallInLove
 * @since 2021-06-16
 */
@RestController
@RequestMapping("/attachment")
@Api(tags = "附件操作控制器")
public class CommonAttachmentController {

    @Autowired
    AttachmentService attachmentService;

    @PostMapping(value = "/uploadAttachment")
    @ApiOperation(value = "上传附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0 老师下发作业 1 学生上传作业 2 老师审批作业 3 学生请假申请", required = true),
            @ApiImplicitParam(name = "homeworkId", value = "提交作业id(三个id选择一个)"),
            @ApiImplicitParam(name = "leaveId", value = "请假id"),
            @ApiImplicitParam(name = "publishHomeworkId", value = "发布作业id")})
    public Page upload(MultipartFile[] files, Integer type, Integer homeworkId, Integer leaveId,
                       Integer publishHomeworkId) {
        return attachmentService.upload(files, type, homeworkId, leaveId, publishHomeworkId);
    }

    @PostMapping("/updateAttachment")
    @ApiOperation(value = "修改附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0 老师下发作业 1 学生上传作业 2 老师审批作业 3 学生请假申请", required = true),
            @ApiImplicitParam(name = "homeworkId", value = "提交作业id(三个id选择一个)"),
            @ApiImplicitParam(name = "leaveId", value = "请假id"),
            @ApiImplicitParam(name = "publishHomeworkId", value = "发布作业id")})
    public Page update(MultipartFile[] files, Integer type, Integer homeworkId, Integer leaveId,
                       Integer publishHomeworkId) {
        return attachmentService.update(files, type, homeworkId, leaveId, publishHomeworkId);
    }

    @GetMapping("/downloadAttachment")
    @ApiOperation(value = "下载附件")
    @ApiImplicitParam(name = "path", value = "文件路径", required = true)
    public ResponseEntity<FileSystemResource> download(String path) {
        return attachmentService.download(path);
    }
}
